package com.qymerge.manage.base.controller;

import com.qymerge.manage.base.dto.TempMsgDto;
import com.qymerge.manage.base.dto.export.TempMsgExportDTO;
import com.qymerge.manage.base.enums.IsuseTypes;
import com.qymerge.manage.base.ro.TempMsgRO;
import com.qymerge.manage.base.service.ITempMsgService;
import com.qymerge.manage.core.bean.*;
import com.qymerge.manage.core.constant.ParamKey;
import com.qymerge.manage.core.controller.BaseController;
import com.qymerge.manage.core.utils.*;
import com.google.common.reflect.TypeToken;
import com.qymerge.manage.core.bean.*;
import com.qymerge.manage.core.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 *<p>Title: TempMsgController</p>
 *<p>Description: H5预约</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/12/19/0019
 *@Time 18:03
 */
@Controller
@RequestMapping(value = "base/tempmsg")
public class TempMsgManageController extends BaseController {

    @Autowired
    private ITempMsgService tempMsgService;

    /**
     * 列表
     *
     * @param parameters
     * @param callback
     */
    @RequestMapping( "list")
    public void list(HttpServletRequest request, HttpServletResponse response,

                     @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                     @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<TempMsgDto> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<TempMsgDto>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        TempMsgDto ro = paramValue.getData();

        DataTableRO data = tempMsgService.list(ro, new PageRequest(common.getPage(), common.getSize()));
        PrintCodeUtils.printSuccess(response, data.build(), callback);
    }


    /**
     * 详情
     *
     * @param request
     * @param response
     * @param callback
     * @throws Exception
     */
    @RequestMapping("detail")
    public void detail(HttpServletRequest request, HttpServletResponse response,

                       @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                       @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {

        ParamValue<TempMsgRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<TempMsgRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        TempMsgRO ro = paramValue.getData();

        RequestCheckUtils.checkNotEmpty(ro.getTempmsgid(), "ID必须");
        TempMsgRO resultRO = tempMsgService.detail(ro.getTempmsgid());
        PrintCodeUtils.printSuccess(response, resultRO, callback);
    }

    /**
     * 增加
     *
     * @param request
     * @param response
     * @param callback
     */
    @RequestMapping("save")
    public void save(HttpServletRequest request, HttpServletResponse response,

                     @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                     @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {
        ParamValue<TempMsgRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<TempMsgRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        TempMsgRO ro = paramValue.getData();

        ActionResultRO resultRO = tempMsgService.save(ro);
        PrintCodeUtils.print(response, resultRO, callback);
    }

    /**
     * 导入execl格式数据
     *
     * @param request
     * @param response
     * @param callback
     * @throws Exception
     */
    @RequestMapping("/importData")
    public void importData(HttpServletRequest request, HttpServletResponse response,
                           @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                           @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {
        ParamValue<CommonIdsDTO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<CommonIdsDTO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        CommonIdsDTO ro = paramValue.getData();

        if (!(request instanceof MultipartHttpServletRequest)) {
            PrintCodeUtils.printError(response, "提交方式必须是 multipart/form-data", callback);
        }
        MultipartFile uploadify = ((DefaultMultipartHttpServletRequest) request).getFile("file");
        if (uploadify == null || uploadify.isEmpty()) {
            PrintCodeUtils.printError(response, "请选择文件", callback);
        }

        ActionResultRO resultDTO = tempMsgService.importData(uploadify, ro.getFlag());
        PrintCodeUtils.print(response, resultDTO, callback);
    }

    /**
     * 导出数据
     *
     * @param parameters
     * @param callback
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/exportData")
    public void exportData(HttpServletRequest request, HttpServletResponse response,

                           @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                           @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {
        parameters = URLDecoder.decode(parameters, "UTF-8");
        ParamValue<TempMsgDto> paramValue = GsonUtil.json2T(parameters, new com.google.gson.reflect.TypeToken<ParamValue<TempMsgDto>>() {
        }.getType());
        TempMsgDto ro = paramValue.getData();

        List<TempMsgExportDTO> data = tempMsgService.exportData(ro);
        ExportExcelUtil<TempMsgExportDTO> exportExcel = new ExportExcelUtil<TempMsgExportDTO>();
        String[] headers = {"编号", "姓名", "电话", "核销码", "是否核销", "核销日期", "添加日期"};
        response.setContentType("application/vnd.ms-excel");

        // 设置下载的文件名
        String codedFileName = URLEncoder.encode("tempmsg" + DateUtil.format(new Date(), "_MMdd_hhmmss"), "UTF-8");
        response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");

        OutputStream fOut = response.getOutputStream();
        exportExcel.exportExcel(null, "tempmsg", headers, data, fOut);
        fOut.flush();
        fOut.close();
        System.out.println("h5预约excel导出成功！");
    }

    /**
     * 删除
     *
     * @param parameters
     * @return
     */
    @RequestMapping(value = "/delete")
    public void delete(HttpServletRequest request, HttpServletResponse response,

                       @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                       @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<CommonIdsDTO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<CommonIdsDTO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        CommonIdsDTO commonIdsDTO = paramValue.getData();

        ActionResultRO obj = tempMsgService.delete(commonIdsDTO.getIds());
        PrintCodeUtils.print(response, obj, callback);
    }

    /**
     * 上线
     *
     * @param parameters
     * @param callback
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/online")
    public void online(@RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                       @RequestParam(defaultValue = ParamKey.In.empty) String callback,
                       HttpServletRequest request, HttpServletResponse response) throws Exception {
        ParamValue<TempMsgRO> paramValue = GsonUtil.json2T(parameters, new com.google.gson.reflect.TypeToken<ParamValue<TempMsgRO>>() {
        }.getType());
        TempMsgRO tempMsgRO = paramValue.getData();

        long id = tempMsgRO.getTempmsgid();
        RequestCheckUtils.checkNotEmpty(id, "预约ID必须");
        ActionResultRO resultRO = tempMsgService.updateIsuse(id, IsuseTypes.OnLine.getStatus(), IsuseTypes.OnLine.getDesc());
        PrintCodeUtils.print(response, resultRO, callback);
    }

    /**
     * 下线
     *
     * @param parameters
     * @param callback
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/offline")
    public void offline(@RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                        @RequestParam(defaultValue = ParamKey.In.empty) String callback,
                        HttpServletRequest request, HttpServletResponse response) throws Exception {
        ParamValue<TempMsgRO> paramValue = GsonUtil.json2T(parameters, new com.google.gson.reflect.TypeToken<ParamValue<TempMsgRO>>() {
        }.getType());
        TempMsgRO skuRO = paramValue.getData();

        long id = skuRO.getTempmsgid();
        RequestCheckUtils.checkNotEmpty(id, "预约ID必须");
        ActionResultRO resultRO = tempMsgService.updateIsuse(id, IsuseTypes.OffLine.getStatus(), IsuseTypes.OffLine.getDesc());
        PrintCodeUtils.print(response, resultRO, callback);
    }


}
